package com.hyfrogx.modules.app.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.qcloud.UploadUtils;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.validator.ValidatorUtils;
import com.hyfrogx.modules.app.annotation.Login;
import com.hyfrogx.modules.app.form.userauther.AuthEditForm;
import com.hyfrogx.modules.app.form.userauther.AuthSaveForm;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.entity.userauth.HyUserAutherEntity;
import com.hyfrogx.modules.manage.service.userauther.HyUserAutherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Li9527
 * @date ：Created in 2019-12-02 17:04
 * @description：${description}
 * @modified By：
 * @version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/auther")
@Api(value = "ApiUserAutherController", tags = "客户端--用户开播认证")
public class ApiUserAutherController {


    @Autowired
    HyUserAutherService hyUserAutherService;


    /**
     * 上传COS
     *
     * @param file
     * @return
     */
    @Login
    @ApiOperation("COS上传 🐉")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        // 文件路径
        String url = "";
        // 腾讯云
        url = UploadUtils.uploadWebFile(file, "/auther");
        return R.ok().put("filename", url);
    }

    /**
     * 客户端提交认证
     *
     * @param form
     * @throws MyException
     */
    @Login
    @PostMapping("/create")
    @ApiOperation("客户端提交认证 🐉")
    public R create(@RequestBody AuthSaveForm form,
                    @ApiIgnore @RequestAttribute(value = "userId") String userId,
                    @ApiIgnore @RequestAttribute(value = "userIp") String userIp) throws MyException {
        ValidatorUtils.validateClientEntity(form);
        HyUserAutherEntity userAutherOld = hyUserAutherService.getOne(new QueryWrapper<HyUserAutherEntity>().eq("uid",userId),false);
        if(userAutherOld != null){
            return  R.error("已提交过认证,只能等待审核失败后再次认证");
        }
        HyUserAutherEntity userAutherEntity = new HyUserAutherEntity();
        BeanUtils.copyProperties(form,userAutherEntity);
        userAutherEntity.setUid(Integer.valueOf(userId));
        userAutherEntity.setCreateDate(new Date());
        userAutherEntity.setStatus(FrogConstant.AutherStatus.WAIT);
        Boolean result = hyUserAutherService.save(userAutherEntity);
        if(result){
            return R.ok("提交审核成功");
        }else{
            return R.error("提交失败");
        }
    }


    /**
     * 获取认证状态,未通过补返失败原因
     * @param userId
     * @return
     */
    @Login
    @ApiOperation("获取认证状态,未通过补返失败原因 🐉")
    @GetMapping("/getStatus")
    public R getStatus(@ApiIgnore @RequestAttribute(value = "userId") String userId) {
        Map<String,Object> map = new HashMap<String, Object>();
        HyUserAutherEntity hyUserAutherEntity = hyUserAutherService.getOne(new QueryWrapper<HyUserAutherEntity>().eq("uid",userId),false);
        if(hyUserAutherEntity != null){
            map.put("status",hyUserAutherEntity.getStatus());
            if(hyUserAutherEntity.getStatus() == FrogConstant.AutherStatus.ERROR){
                map.put("cause",hyUserAutherEntity.getCause());
            }
            return  R.ok().put("data",map);
        }else{
            map.put("status",FrogConstant.AutherStatus.NOAUTH);
            return R.ok().put("data",map);
        }
    }


    /**
     * 客户端重新提交认证
     *
     * @param form
     * @throws MyException
     */
    @Login
    @PostMapping("/retry")
    @ApiOperation("客户端重新提交认证 🐉")
    public R create(@RequestBody AuthEditForm form,
                    @ApiIgnore @RequestAttribute(value = "userId") String userId,
                    @ApiIgnore @RequestAttribute(value = "userIp") String userIp) throws MyException {
        ValidatorUtils.validateClientEntity(form);
        HyUserAutherEntity userAutherEntity = hyUserAutherService.getOne(new QueryWrapper<HyUserAutherEntity>().eq("uid",userId),false);
        BeanUtils.copyProperties(form,userAutherEntity);
        userAutherEntity.setStatus(FrogConstant.AutherStatus.WAIT);
        Boolean result = hyUserAutherService.updateById(userAutherEntity);
        if(result){
            return R.ok("提交审核成功");
        }else{
            return R.error("提交失败");
        }
    }


}
